//
//  LCHomeViewController.m
//  EnterpriseCloud
//
//  Created by 东亨 on 2017/7/4.
//  Copyright © 2017年 lichao. All rights reserved.
//

#import "LCHomeViewController.h"
#import "LCServiceCell.h"
#import "LCHeadView.h"
#import "LCDetailsViewController.h"
#import "RDVTabBarController.h"
#import "LCMessageViewController.h"
#import "LCLogisticsViewController.h"
#import "LCSocialViewController.h"
#import "LCPartyViewController.h"
#import "ListModel.h"
#import "ToastUtils.h"
#import "LCLoginViewController.h"
#import "LCEnrollmentViewController.h"
static NSString *cellIdentifier = @"LCServiceCell";
@interface LCHomeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIView *headerView;
@property (strong,nonatomic)NSMutableDictionary  *dataDic;
@property (strong,nonatomic)NSMutableArray       *dataArray;

@end
@implementation LCHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBarTitle:@"首页"];
    _dataDic = [[NSMutableDictionary alloc]initWithCapacity:0];
    _dataArray = [[NSMutableArray alloc]initWithCapacity:0];
    [self loadCustomeView];
}
- (void)loadCustomeView
{
    CGFloat height =SCREEN_WIDTH*(188.0/375)+40+3*((SCREEN_WIDTH-40)/2.0*(3.0/8));
    _headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, height);
    _tableView.tableHeaderView = _headerView;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc]init];
    [_tableView registerNib:[UINib nibWithNibName:@"LCServiceCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    [_tableView.mj_header beginRefreshing];
}
-(void)loadData
{
    [_dataArray removeAllObjects];
    [_dataDic setObject:@"2" forKey:@"column_id"];
    [_dataDic setObject:@"3" forKey:@"num"];
    [MDataProvider homeList:_dataDic sucess:^(NSArray *array)
    {
        [_dataArray addObjectsFromArray:array];
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
    } faile:^(NSString *str) {
        [_tableView.mj_header endRefreshing];
        [SVProgressHUD showErrorWithStatus:str];
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    LCHeadView *view = [[LCHeadView alloc]initWithTitle:@"政策服务"];
    return view;
}
#pragma mark -UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LCServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (_dataArray.count > 0)
    {
        ListModel *model = [_dataArray objectAtIndex:indexPath.row];
        cell.titleLbl.text = model.title;
        cell.contentLbl.text = model.summary;
        cell.timeLbl.text = model.published_date;
        NSString *imgUrl = [NSString stringWithFormat:@"%@/%@%@",ENCLOUD_URL,model.file_path,model.file_name];
        [cell.iconImg sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
        cell.stateLbl.hidden = YES;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //详情页面
    LCDetailsViewController *vc = [[LCDetailsViewController alloc]init];
    ListModel *model = [_dataArray objectAtIndex:indexPath.row];
    vc.info_id = model.info_id;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)stateBtnClick:(UIButton *)sender
{
    switch (sender.tag) {
        case 1:
        {
            //党建栏目
            LCPartyViewController *vc = [[LCPartyViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 2:
        {
            //社工服务
            LCSocialViewController *vc = [[LCSocialViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 3:
        {
            if (!get_sp(@"app_user"))
            {
                [ToastUtils show:@"请您登录后进行此项操作！"];
                //没有登录要先登录
                LCLoginViewController *vc = [[LCLoginViewController alloc]init];
                [self presentViewController:vc animated:YES completion:nil];
                return;
            }
            //物业服务
            LCLogisticsViewController *vc = [[LCLogisticsViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 4:
        {
            //咨讯服务
            [self rdv_tabBarController].selectedIndex = 2;
            break;
        }
        case 5:
        {
            //留言建议
            if (!get_sp(@"app_user"))
            {
                [ToastUtils show:@"请您登录后进行此项操作！"];
                //没有登录要先登录
                LCLoginViewController *vc = [[LCLoginViewController alloc]init];
                [self presentViewController:vc animated:YES completion:nil];
                return;
            }
            LCMessageViewController *vc = [[LCMessageViewController alloc]init];
            vc.typeStr = @"留言";
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 6:
        {
//            活动报名
            LCEnrollmentViewController *vc = [[LCEnrollmentViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
//            LCNewDetailViewController *vc = [[LCNewDetailViewController alloc]init];
//            [self.navigationController pushViewController:vc animated:YES];
            
            break;
        }
        default:
            break;
    }
}
@end
